<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('姓名')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('姓名')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('性别')">
            <el-radio-group v-model="form.sex">
              <el-radio-button
                v-for="dict in sexOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('手机号码')" prop="phone">
            <el-input v-model="form.phone" :placeholder="$t('手机号码')" show-word-limit :maxlength="11" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('邮箱')" prop="email">
            <el-input v-model="form.email" :placeholder="$t('邮箱')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('微信账户')" prop="weChat">
            <el-input v-model="form.weChat" maxlength="30" show-word-limit :placeholder="$t('微信账户')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出生年月')" prop="birthDate">
            <el-date-picker
              v-model="form.birthDate"
              clearable
              style="width: 200px"
              type="month"
              value-format="yyyy-MM-dd"
              :placeholder="$t('出生年月')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系人类型')" prop="personType">
            <dict-select v-model="form.personType" disabled dictionary="contact_type" :placeholder="$t('联系人类型')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('身份证号码')" prop="idCard">
            <el-input v-model="form.idCard" :placeholder="$t('身份证号码')" show-word-limit :maxlength="18" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('学历')" prop="education">
            <el-input v-model="form.education" :placeholder="$t('学历')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('职称')" prop="title">
            <el-input v-model="form.title" :placeholder="$t('职称')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('工作单位')" prop="workUnit">
            <el-input v-model="form.workUnit" :placeholder="$t('工作单位')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('行业')" prop="industry">
            <el-input v-model="form.industry" :placeholder="$t('行业')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('专长')" prop="expertise">
            <el-input v-model="form.expertise" :placeholder="$t('专长')" show-word-limit :maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('所属公司')" prop="company">
            <el-input v-model="form.company" :placeholder="$t('所属公司')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('受教育经历')" prop="educationalExperience">
            <el-input v-model="form.educationalExperience" type="textarea" :placeholder="$t('受教育经历')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('工作简历')" prop="resume">
            <el-input v-model="form.resume" type="textarea" :placeholder="$t('工作简历')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('工作业绩')" prop="performance">
            <el-input v-model="form.performance" type="textarea" :placeholder="$t('工作业绩')" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn has-permi="deal:experts" />
    </div>
  </div>
</template>

<script>
    import { saveExpertsBase, getExpertsBase } from '@/api/deal/experts/expertsBase'
    export default {
        data() {
          // 电话验证
          var validatePhone = (rule, value, callback) => {
            if (!value) {
              callback(new Error(this.$t('必填信息')))
            } else {
              var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (!reg.test(value)) {
                callback(new Error(this.$t('手机号格式错误')))
              } else {
                callback()
              }
            }
          }
            return {
                // 性别字典
                sexOptions: [], // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  name: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], phone: [
                    { required: true, validator: validatePhone, trigger: 'blur' }
                  ], email: [
                    { type: 'email', message: this.$t('邮箱格式错误'), trigger: 'blur' }
                  ], idCard: [
                    { pattern: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/, message: this.$t('正确的身份证号'), trigger: 'blur' }
                  ]
                }
            }
        },
        created() {
            this.getDicts('sys_user_sex').then(response => {
                this.sexOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getExpertsBase(data.id).then(response => {
                        this.form = response.data
                      this.loading = false
                    })
                } else {
                    this.reset()
                  this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                name: undefined,
                sex: '0',
                phone: undefined,
                email: undefined,
                idCard: undefined,
                education: undefined,
                title: undefined,
                workUnit: undefined,
                industry: undefined,
                expertise: undefined,
                educationalExperience: undefined,
                resume: undefined,
                performance: undefined,
                createBy: undefined,
                updateBy: undefined,
                createTime: undefined,
                updateTime: undefined,
                delFlag: undefined,
                  weChat: undefined,
                  birthDate: undefined,
                  personType: 'ZYLXR',
                  remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveExpertsBase(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }
        }

    }
</script>
